﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using BBCP.Model;

namespace BBCP.UI.Tasks
{
    class CustommersSummary : ITask
    {
        public bool Run()
        {
            using (NorthwindEntities context = new NorthwindEntities())
            {

                Console.WriteLine("klienci GOLD – zakupy za kwotę powyżej 100 000 USD:  ");
                context.Customers.Select(n => n)
                    .Where(n => n.Orders.Sum(m => m.Order_Details
                        .Sum(od => od.Quantity * (float)od.UnitPrice - (od.Quantity * (float)od.UnitPrice * (float)od.Discount))) > 100000)
                        .ToList().ForEach(n =>
                        {
                            var sumary = n.Orders.Sum(m => m.Order_Details
                        .Sum(od => od.Quantity * (float)od.UnitPrice - (od.Quantity * (float)od.UnitPrice * (float)od.Discount)));
                            Console.WriteLine(string.Format("Nazwa: {0,-40} Suma: {1}", n.CompanyName, sumary ));
                        });
                Console.WriteLine(Environment.NewLine);
                Console.WriteLine("Klienci SILVER – zakupy za kwotę powyżej 10 000 USD:");
                context.Customers.Select(n => n)
                    .Where(n => n.Orders.Sum(m => m.Order_Details
                        .Sum(od => od.Quantity * (float)od.UnitPrice - (od.Quantity * (float)od.UnitPrice * (float)od.Discount))) < 100000
                        && n.Orders.Sum(m => m.Order_Details
                        .Sum(od =>od.Quantity * (float)od.UnitPrice - (od.Quantity * (float)od.UnitPrice * (float)od.Discount))) > 10000)
                        .ToList().ForEach(n =>
                        {
                            var sumary = n.Orders.Sum(m => m.Order_Details
                        .Sum(od => od.Quantity * (float)od.UnitPrice - (od.Quantity * (float)od.UnitPrice * (float)od.Discount)));
                            Console.WriteLine(string.Format("Nazwa: {0,-40} Suma: {1}", n.CompanyName, sumary));  
                            
                          
                        });
                Console.WriteLine(Environment.NewLine);
                Console.WriteLine("Klienci NORMAL - zakupy poniżej 10 000 USD:");
                context.Customers.Select(n => n)
                  .Where(n => n.Orders.Sum(m => m.Order_Details
                      .Sum(od => od.Quantity * (float)od.UnitPrice - (od.Quantity * (float)od.UnitPrice * (float)od.Discount))) < 10000)
                      .ToList().ForEach(n =>
                      {
                          var sumary = n.Orders.Sum(m => m.Order_Details
                        .Sum(od => od.Quantity * (float)od.UnitPrice - (od.Quantity * (float)od.UnitPrice * (float)od.Discount)));
                          Console.WriteLine(string.Format("Nazwa: {0,-40} Suma: {1}", n.CompanyName, sumary));
                          
                          //    Console.WriteLine("Name: " + n.CompanyName + "\t\tsuma zamowien: " + n.Orders.Sum(m => m.Order_Details
                     // .Sum(od => od.Quantity * (float)od.UnitPrice - (od.Quantity * (float)od.UnitPrice * (float)od.Discount))));
                      });

                return true;
            }
        }
    }
}
